package com.xq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xq.dto.DmsDevice.DmsDeviceListDTO;
import com.xq.dto.DmsDevice.DmsDeviceTypeInsertDTO;
import com.xq.dto.DmsDevice.DmsDeviceTypeListDTO;
import com.xq.entity.DmsDeviceTypes;
import com.xq.exception.BusinessException;
import com.xq.mapper.DmsDeviceTypesMapper;
import com.xq.query.DmsDeviceTypeListQuery;
import com.xq.service.IDmsDeviceTypesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.vo.DataBaseEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuven
 * @since 2025-07-03
 */
@Service
public class DmsDeviceTypesServiceImpl extends ServiceImpl<DmsDeviceTypesMapper, DmsDeviceTypes> implements IDmsDeviceTypesService {

    @Autowired
    private DmsDeviceTypesMapper dmsDeviceTypesMapper;

    @Override
    public void insertDeviceTypes(DmsDeviceTypeInsertDTO dmsDeviceTypeInsertDTO) {
        DmsDeviceTypes dmsDeviceTypes = new DmsDeviceTypes();
        dmsDeviceTypes.setTypeName(dmsDeviceTypeInsertDTO.getTypeName());
        dmsDeviceTypes.setProtocolType(dmsDeviceTypeInsertDTO.getProtocolType());
        dmsDeviceTypes.setDescription(dmsDeviceTypeInsertDTO.getDescription());
        dmsDeviceTypes.setFields(dmsDeviceTypeInsertDTO.getFields());
        int insert = dmsDeviceTypesMapper.insert(dmsDeviceTypes);
        if (insert == 0) {
            throw new BusinessException(DataBaseEnum.INSERTION_FAILED);
        }
    }

    @Override
    public List<DmsDeviceTypeListDTO> getDeviceTypeList(DmsDeviceTypeListQuery dmsDeviceTypeListQuery) {

        Page<DmsDeviceTypes> page=new Page<>(dmsDeviceTypeListQuery.getPageIndex(),dmsDeviceTypeListQuery.getPageSize());
        QueryWrapper<DmsDeviceTypes> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("type_name",dmsDeviceTypeListQuery.getTypeName()).
                like("protocol_type",dmsDeviceTypeListQuery.getProtocolType());
        Page<DmsDeviceTypes> dmsDeviceTypesPage = dmsDeviceTypesMapper.selectPage(page, queryWrapper);
        List<DmsDeviceTypes> list = dmsDeviceTypesPage.getRecords();
        List<DmsDeviceTypeListDTO> dmsDeviceTypeListDTOS=new ArrayList<>();
        for (DmsDeviceTypes dmsDeviceTypes : list) {
            DmsDeviceTypeListDTO dmsDeviceTypeListDTO=new DmsDeviceTypeListDTO();
            dmsDeviceTypeListDTO.setTypeName(dmsDeviceTypes.getTypeName());
            dmsDeviceTypeListDTO.setProtocolType(dmsDeviceTypes.getProtocolType());
            dmsDeviceTypeListDTO.setDescription(dmsDeviceTypes.getDescription());
            dmsDeviceTypeListDTO.setFields(dmsDeviceTypes.getFields());
            dmsDeviceTypeListDTOS.add(dmsDeviceTypeListDTO);
        }
        return dmsDeviceTypeListDTOS;

    }
}
